package com.atguigu.crowd.handler;

import com.atguigu.crowd.api.MySQLRemoteService;
import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.entity.vo.PortalTypeVO;
import com.atguigu.crowd.util.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.List;

@Controller
public class PortalHandler {

   Logger logger = LoggerFactory.getLogger(PortalHandler.class);

    @Autowired
    private MySQLRemoteService mySQLRemoteService;

    @RequestMapping("/")
    public String showPortalPage(Model model) {

        //调用mySQLRemoteService提供的方法查询首页要显示的数据
        ResultEntity<List<PortalTypeVO>> portalTypeProjectDataResultEntity = mySQLRemoteService.getPortalTypeProjectDataRemote();
        //logger.info(portalTypeProjectDataResultEntity.toString());
        //检查查询结果
        String result = portalTypeProjectDataResultEntity.getResult();
        if (ResultEntity.SUCCESS.equals(result)){
            //获取查询结果的数据
            List<PortalTypeVO> portalTypeVOList = portalTypeProjectDataResultEntity.getData();
            //Arrays.asList(portalTypeVOList).forEach(System.out::println);
            //存入模型
            model.addAttribute(CrowdConstant.ATTR_NAME_PORTAL_DATA,portalTypeVOList);
        }

        return "portal";
    }

}
